/**
 * author : 김기만 최초 페이지
 */

function clsNavi() {
    // Util Class
    this.mUtil = null;

    // UI Select 데이터
    this._HTMLDom = null;

}

clsNavi.prototype = {
    /**
     * 유저 데이터 요청
     */
    getProfile : function() {
        var thisClass = this;
        var objJson = new MJson();
        var cm = new ConnectionManager();

        objJson.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);

        objJson.putPub(MJson.PUB.SID, "US001");
        objJson.putPub(MJson.PUB.SERVICENAME, "getMyProfile");

        cm.requestGet(objJson.getJsonString(), function($jsonData) {
            var objJson = new MJson();

            // alert("getProfile success :: " + JSON.stringify($jsonData));
            // alert("success check :: " + objJson.checkResult($jsonData));
            if (objJson.checkResult($jsonData)) {
                var item = $jsonData.msp.contents.items;
                thisClass.setViewProfile(item);
            } else {
                console.log("getProfile error");
                console.log($jsonData);
            }
        }, function(e) {
            alert("fail :: " + JSON.stringify(e));
            $.mobile.hidePageLoadingMsg();
        });
    },
    /**
     * 유저 프로필 표시
     */
    setViewProfile : function($data) {
        var data = $data;
        var profile_layer = this._HTMLDom.profile;
        var img = profile_layer.find('img');
        var nick = profile_layer.find('span');

        // 닉네임
        if (data.nickname != undefined)
            nick.html(data.nickname);
        // 유저 사진
        if (data.saveFileName != undefined)
            img.attr('src', this.mUtil.filePath2Url(data.path) + data.saveFileName);
    },

    getGroupMenu : function(group) {
        //$("a").attr("href","");
        if (!group)
            return;
        if (group == "topmenu") {
            return $("section header")
        } else if (group == "joinroom") {
            return $("#menu_list ul:eq(0) li");
        } else if (group == "booking") {
            return $("#menu_list ul:eq(1) li");
        } else if (group == "club") {
            return $("#menu_list ul:eq(2) li");
        } else if (group == "course") {
            return $("#menu_list ul:eq(3) li");
        } else if (group == "mypage") {
            return $("#menu_list ul:eq(4) li");
        } else if (group == "qna") {
            return $("#menu_list ul:eq(5) li");
        } else if (group == "sett") {
            return $("#menu_list ul:eq(6) li");
        }
    },
    getTopMenu : function() {
        var thisClass = this;
        var $topmenu = this.getGroupMenu("topmenu");
        var $anchor = $topmenu.find("a").attr("href", "");

        if (document.globalObj.LOGIN_DATA.isLogin) {
            // 로그인 되었을 때
            this._HTMLDom.btn_join.hide();
            this._HTMLDom.btn_login.hide();
            // 유저 데이터 요청
            this.getProfile();
        } else {
            // 로그인이 안되어 있을 때
            this._HTMLDom.btn_home.hide();
            this._HTMLDom.profile.hide();
        }

        $anchor.click(function(event) {
            var _url = "";

            switch (event.currentTarget.name) {
                case 'loginBtn' :
                    _url = "../member/login.html";
                    break;
                case 'joinBtn':
                    _url = "../member/join.html";
                    break;
                case 'profileBtn' :
                    document.globalObj.PROFILE_DATA.userId = document.globalObj.LOGIN_DATA.userId;
                    _url = "../mypage/profile_view.html";
                    break;
                case 'homeBtn' :
                    _url = "../index/my_index.html";
                    break;
            }
            thisClass.goPage(_url);
            console.log();
        });

    },
    getJoinroom : function() {
        var thisClass = this;
        var $joinroom = this.getGroupMenu("joinroom");

    },
    getBooking : function() {
        var thisClass = this;
        var $booking = this.getGroupMenu("booking");
        $booking.find("a").attr("href", "");
        //$mypage.unbind("click");

        // TODO :: 웨이팅 예약은 DCD  작업 이후 표시
        $booking.eq(1).hide();

        $booking.click(function(event) {
            var _url = "";
            var idx = $booking.index($(this));
            switch(idx) {
                case 0 :
                    document.globalObj.BOOKING_DATA.category.searchReservation = 0;
                    document.globalObj.BOOKING_DATA.calendar.selectDate = new Array();
                    _url = "../booking/calendar.html";
                    break;
                case 1 :
                    document.globalObj.BOOKING_DATA.category.searchReservation = 1;
                    document.globalObj.BOOKING_DATA.calendar.selectDate = new Array();
                    _url = "../booking/calendar.html";
                // break;
                case 2 :

                    if (document.globalObj.LOGIN_DATA.isLogin == false) {
                        console.log('login_donation');
                        document.globalObj.LOGIN_DATA.loginAfterUrl = '../booking/donation.html';
                        $.mobile.changePage("../member/login.html");
                    } else {
                        console.log('donation');
                        _url = "../booking/donation.html";
                    }

                    break;
            }
            thisClass.goPage(_url);
        });

    },
    getClub : function() {
        var thisClass = this;
        var $club = this.getGroupMenu("club");
        $club.find("a").attr("href", "");
        $club.click(function(event) {
            //alert("동호회는 준비중입니다.");            
            var _url = "../club/club.html";
            thisClass.goPage(_url);
        });
    },
    getCourse : function() {
        var thisClass = this;
        var $course = this.getGroupMenu("course");
        $course.find("a").attr("href", "");
        $course.click(function(event) {
            var _url = "../course/course.html";

            thisClass.goPage(_url);
        });
    },
    getMypage : function() {
        var thisClass = this;
        var $mypage = this.getGroupMenu("mypage");
        $mypage.find("a").attr("href", "");

        // 로그인이 안되어 있을 때 Hide 처리
        if (!document.globalObj.LOGIN_DATA.isLogin) {
            $mypage.parent().hide();
            return;
        }

        //$mypage.unbind("click");
        $mypage.click(function(event) {
            var _url = "";
            var idx = $mypage.index($(this));
            switch(idx) {
                case 0 :
                    _url = "../mypage/my_score.html";
                    break;
                case 1 :
                    _url = "../mypage/join_info.html";
                    break;
                case 2 :
                    _url = "../mypage/booking_info.html";
                    break;
                case 3 :
                    _url = "../mypage/mileage.html";
                    break;
                case 4 :
                    _url = "../mypage/modify.html";
                    break;
            }
            thisClass.goPage(_url);
        });
    },
    getQna : function() {
        var thisClass = this;
        var $qna = this.getGroupMenu("qna");
        $qna.find("a").attr("href", "");
        $qna.click(function(event) {
            var _url = null;

            _url = "../customer/customer.html";

            thisClass.goPage(_url);
        });
    },
    getSetting : function() {
        var thisClass = this;
        var $sett = this.getGroupMenu("sett");
        $sett.find("a").attr("href", "");
        $sett.click(function(event) {
            var _url = null;

            if (document.globalObj.LOGIN_DATA.isLogin) {
                _url = "../inc/setup_out.html";
            } else {
                _url = "../inc/setup.html";
            }

            thisClass.goPage(_url);
        });
    },
    goPage : function(_url) {
        $.mobile.changePage(_url, {
            transition : "none"
        }, true);
    },
    onPageShow : function() {
        this.mUtil = document.globalObj.util;

        var header_layer = $('header');

        this._HTMLDom = {
            // 회원가입
            "btn_join" : header_layer.find('h6:first'),
            // 로그인
            "btn_login" : header_layer.find('div:first'),
            // 홈 버튼
            "btn_home" : header_layer.find('h1:first'),
            // 유저 이름 및 사진
            "profile" : header_layer.find('div:last')
        };

        this.getTopMenu();
        this.getMypage();
        //            this.getJoinroom();
        this.getBooking();
        this.getClub();
        this.getCourse();
        //            this.getQna();
        this.getSetting();

    }
}
